home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-02 / sp12src.zip / MAKEDICT.PAS < prev    next >
Pascal/Delphi Source File  |  1991-03-27  |  4KB  |  124 lines

  1. {$A+,B-,D-,E-,F+,G-,I+,L-,N-,O-,R-,S-,V+,X+}
  2. {$M 16384,0,655360}
  3. Program MakeDict;
  4. { MAKEDICT - A dictionary builder.  Copyright (c) 1990,91 by Edwin T. Floyd.
  5.   Build an optimal dictionary from an input word list.  The input word list
  6.   should contain one word per line.  The output dictionary is saved to disk.
  7.   Run MAKEDICT with no parameters for brief documentation. }
  8. Uses Dict, Crt, Dos;
  9. Const
  10.   DefaultBits = 14;      { Default number of bits per word }
  11.  
  12. Var
  13.   Words : LongInt;       { Number of input words }
  14.   ExtraWords : LongInt;  { Number of extra words to allow in dictionary }
  15.   Bits : Byte;           { Number of bits to represent each word }
  16.   InFileName : PathStr;  { Input file name }
  17.   OutFileName : PathStr; { Output file name }
  18.   InBuf : Array[1..16384] Of Char; { I/O buffer for input }
  19.  
  20. Procedure InterpretParam;
  21. { Interpret input parameters }
  22. Var
  23.   i : Integer;
  24.   p : PathStr;
  25.   d : DirStr;
  26.   n : NameStr;
  27.   e : ExtStr;
  28. Begin
  29.   WriteLn('MAKEDICT v1.2 - A dictionary builder.  Copyright (c) 1990,91 by Edwin T. Floyd.');
  30.   If ParamCount < 1 Then Begin
  31.     WriteLn('Run like this: ');
  32.     WriteLn;
  33.     WriteLn('  MAKEDICT <infile> [<bits>] [<extra>]');
  34.     WriteLn;
  35.     WriteLn('<infile> specifies the input file name, and optional <bits>');
  36.     WriteLn('specifies the number of bits to use for each word.  If <bits>');
  37.     WriteLn('is omitted, the program will use ', DefaultBits,
  38.       ' bits per word.  Optional');
  39.     WriteLn('<extra> is the number of extra words to allow for in the');
  40.     WriteLn('dictionary.  The output file has the same name as the input');
  41.     WriteLn('file, but the extension will be ".DCT" (or ".DIC" if the input');
  42.     WriteLn('file extension is ".DCT").  Examples:');
  43.     WriteLn;
  44.     WriteLn('  MAKEDICT atob.txt 15');
  45.     WriteLn('  MAKEDICT ctod.txt');
  46.     WriteLn('  MAKEDICT user.lst 12 2000');
  47.     Halt(1);
  48.   End;
  49.   p := FExpand(ParamStr(1));
  50.   FSplit(p, d, n, e);
  51.   InFileName := p;
  52.   If e = 'DCT' Then e := 'DIC' Else e := 'DCT';
  53.   OutFileName := n + '.' + e;
  54.   If ParamCount > 1 Then Begin
  55.     Val(ParamStr(2), Bits, i);
  56.     If (i <> 0) Or (Bits < 1) Then Begin
  57.       WriteLn('Bits value is ', ParamStr(2), ', should be 1..255');
  58.       Halt(1);
  59.     End;
  60.   End Else Bits := DefaultBits;
  61.   If ParamCount > 2 Then Begin
  62.     Val(ParamStr(3), ExtraWords, i);
  63.     If (i <> 0) Or (ExtraWords < 0) Then Begin
  64.       WriteLn('Extra words value is ', ParamStr(2), ', should be numeric > 0');
  65.       Halt(1);
  66.     End;
  67.   End Else ExtraWords := 0;
  68. End;
  69.  
  70. Procedure CountWords;
  71. { Read the input file and count the words }
  72. Var
  73.   InFile : Text;
  74.   s : String;
  75. Begin
  76.   Assign(InFile, InFileName);
  77.   SetTextBuf(InFile, InBuf);
  78.   Reset(InFile);
  79.   Words := 0;
  80.   Repeat
  81.     ReadLn(InFile, s);
  82.     If s <> '' Then Inc(Words);
  83.   Until Eof(InFile);
  84.   Close(InFile);
  85. End;
  86.  
  87. Procedure BuildDictionary;
  88. { Read the input file and build the dictionary }
  89. Var
  90.   Count : LongInt;
  91.   i : Word;
  92.   d : Dictionary;
  93.   InFile : Text;
  94.   s : String;
  95. Begin
  96.   Assign(InFile, InFileName);
  97.   SetTextBuf(InFile, InBuf);
  98.   Reset(InFile);
  99.   Count := 0;
  100.   d.Init(Words + ExtraWords, Bits);
  101.   WriteLn('Reading ', InFileName, ' for ', Words, ' words, ', Bits, ' bits');
  102.   Repeat
  103.     ReadLn(InFile, s);
  104.     Inc(Count);
  105.     If (Count Mod 1000) = 0 Then Write(^M, Count);
  106.     If d.InsertString(s) Then
  107.       WriteLn(^M, Count, ' ', s, ' appears to be already in the dictionary.');
  108.   Until Eof(InFile);
  109.   Close(InFile);
  110.   WriteLn(^M, Count, ' words read ', d.DictCount,
  111.     ' words inserted in dictionary');
  112.   WriteLn('Estimated error chance: 1/', 1/d.EstError:1:0);
  113.   WriteLn('Actual error chance: 1/', 1/d.ActError:1:0);
  114.   WriteLn('Saving ', OutFileName, ', ', d.DictionarySize, ' bytes');
  115.   d.SaveDictionary(OutFileName);
  116.   d.Done;
  117. End;
  118.  
  119. Begin
  120.   InterpretParam;
  121.   CountWords;
  122.   BuildDictionary;
  123. End.
  124.